<?php
/**
 * Created by PhpStorm.
 * User: swy
 * Date: 2015/10/22
 * Time: 20:03
 */
class address extends adminBase{
    function __construct(){
        parent::__construct();
        $this->cms->setPageTitle("地址管理");
        $admin_info=$this->user->getLoginInfo();
        $this->area = AreaModule::init();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(17,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function getAreaMap($id){
        // 生成地址二维数组
        $areaTree = $this->area->getAll($id);
        return $areaTree;
    }

    function indexTask($id=0){
        $r['catTree']=$this->getAreaMap($id);
        $r['pid']=$id;
        $this->cms->tableScene($r,"admin/tpl/address/address.php");
    }

    function addTask($pid){
        $r['pid']=$pid;
        $this->cms->formScene($r,"admin/tpl/address/address_add.php");
    }

    function addSubmitTask($data){
        $nameStr=$data['name'];
        $names=explode("\n",$nameStr);
        foreach($names as $eachName){
            $data['name']=$eachName;
            if($this->area->add($data)){
                $r['url']=WebRouter::init()->getAction("address",'id='.$data['pid']);
                $r['message']="地址添加成功";
                View::displayAsHtml($r,"plugin/state/tips.php");
            }
        }
    }

    function modifyTask($id){
        $r=$this->area->getDetail($id);
//        var_dump($r);
        $this->cms->formScene($r,"admin/tpl/address/address_modify.php");
    }

    function modifySubmitTask($data){
        if($this->area->modify($data)){
            $r['message']="地址修改成功";
            $r['url']=WebRouter::init()->getAction("address",'id='.$data['pid']);
        }else{
            $r['message']="修改失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function deleteTask($id){
        $area_son = $this->area->getSon($id);
        $area_par = $this->area->getFather($id);
        if(!empty($area_son)){
            $r['url']=WebRouter::init()->getAction("address",'id='.$area_par['id']);
            $r['message']="该级地址不能被删除";
        }else{
            if($this->area->delete($id)){
                $r['url']=WebRouter::init()->getAction("address",'id='.$area_par['id']);
                $r['message']="地址删除成功";
            }else{
                $r['message']="删除失败，请重试！";
            }
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
}